package com.education.platform.api.job;


import com.education.platform.api.constants.RedisCacheConstant;
import com.education.platform.api.service.*;
import com.education.platform.api.service.common.CacheDataService;
import com.education.platform.common.redis.RedisUtils;
import com.education.platform.dao.entity.ArticleDO;
import com.education.platform.dao.entity.CourseDO;
import com.education.platform.dao.entity.UserBaseDO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 定时任务
 *
 * @author tomato
 */
@Configuration
@EnableScheduling
public class ScheduleTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleTask.class);

    @Autowired
    private CacheDataService cacheDataService;
    @Autowired
    private UserBaseService userBaseService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserCountService userCountService;

    /**
     * 每天0点初始化每日相关积分
     */
    @Scheduled(cron ="0 0 0 * * ?")
    public void initTodayLearnTime() {
        long start = System.currentTimeMillis();
        cacheDataService.initRedisTodayCount();
        LOGGER.info("初始化redis今日统计用时：" + (System.currentTimeMillis()-start));
        int n = userCountService.initTodayLearnTime();
        LOGGER.info("初始化今日学习时间数：" +n);
    }

    /**
     * 同步文章，课程的点赞 收藏数 到数据库
     */
    @Scheduled(cron ="0 */10 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void syncLikeCollectCount() {
        LOGGER.info("开始更新点赞收藏数----------------");
        long start = System.currentTimeMillis();
        //文章收藏数
        Map<String,String> articleCollectMap=redisUtils.hgetAllStr(RedisCacheConstant.KEY_ARTICLE_COLLECT);
        //文章点赞数
        Map<String,String> articleLikeMap=redisUtils.hgetAllStr(RedisCacheConstant.KEY_ARTICLE_LIKE);
        //课程收藏数
        Map<String,String> courseCollectMap=redisUtils.hgetAllStr(RedisCacheConstant.KEY_COURSE_COLLECT);
        //课程点赞数
        Map<String,String> courseLikeMap=redisUtils.hgetAllStr(RedisCacheConstant.KEY_COURSE_LIKE);


        List<ArticleDO> updateList = new ArrayList<>();
        if(articleCollectMap!=null &&articleCollectMap.size()>0){
            articleCollectMap.forEach((k,v)->{
                ArticleDO  articleDO = new ArticleDO();
                articleDO.setId(Integer.valueOf(k));
                articleDO.setCollectCount(Integer.valueOf(v));
                updateList.add(articleDO);
            });
            articleService.updateInBatch(updateList);
        }
        if(articleLikeMap!=null &&articleLikeMap.size()>0){
            updateList.clear();
            articleLikeMap.forEach((k,v)->{
                ArticleDO  articleDO = new ArticleDO();
                articleDO.setId(Integer.valueOf(k));
                articleDO.setLikeCount(Integer.valueOf(v));
                updateList.add(articleDO);
            });
            articleService.updateInBatch(updateList);
        }
        List<CourseDO> courseDOS = new ArrayList<>();
        if(courseCollectMap!=null &&courseCollectMap.size()>0){
            courseCollectMap.forEach((k,v)->{
                CourseDO  update = new CourseDO();
                update.setId(Integer.valueOf(k));
                update.setCollectCount(Integer.valueOf(v));
                courseDOS.add(update);
            });
            courseService.updateInBatch(courseDOS);
        }
        if(courseLikeMap!=null &&courseLikeMap.size()>0){
            courseDOS.clear();
            courseLikeMap.forEach((k,v)->{
                CourseDO  update = new CourseDO();
                update.setId(Integer.valueOf(k));
                update.setLikeCount(Integer.valueOf(v));
                courseDOS.add(update);
            });
            courseService.updateInBatch(courseDOS);
        }
        LOGGER.info("更新点赞收藏数用时：" + (System.currentTimeMillis()-start));
    }
}